package com.fairytask.engine.components.window
{
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.managers.ResManager;
	import com.fairytask.resource.StaticIni;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	/**
	 * 窗体组件，用于面板背景。
	 * 其它显示对象一般是加入主窗体。 
	 * @author Hanjy
	 * 
	 */
	public class Window extends BaseComponent
	{
		//组件设定
		private var _cHeight	:uint;
		private var _cWidth	:uint;
		private var _titleRes	:String;
		private var _skinbut:String;
		private var _skinHelp:String;
		//容器
		public var _bgSpace		:Sprite;
		private var _titleSpace	:Clip;
		public var _closeBtn		:Button;
		private var _helpBtn		:Button;
		private var _source	:String;
		
		//数值参数
		private var _title			:String;
		private var _width			:Number;
		private var _height		:Number;
		private var _offsetX		:Number = 7;//与原点比水平偏移量
		private var _offsetY		:Number = 9;//与原点比垂直偏移量
		
		private var _titleX		:int;
		private var _titleY		:int;
		private var _canDrag		:Boolean = true;
		
		public var _hasBorderSpace:int;//是否背景边框有边缘(默认UI有)
		
		/**
		 * 构造函数，设定窗体基本参数 
		 * @param title 标题资源
		 * @param w 宽
		 * @param h 高
		 * @param res 皮肤
		 * 
		 */
		public function Window(title	:String 	= "",
							   w		:uint 		= 50, 
							   h		:uint 		= 20,
							   res		:String 	= null,
							   resbut:String=null,
								resHelp:String=null)
		{
			_titleRes	=	title;
			//默认ui都是有边缘的，在ui设计的时候按框线位置算
			_cWidth		=	w + _offsetX * 2;
			_cHeight	=	h + _offsetY * 2;

			_skin		=	res;
			_skinbut= resbut;
			_skinHelp=resHelp;
			if(_skin!=null)
			{
				initGUI();
			}
		}
		
		/**
		 * 创建窗体显示 
		 * 
		 */
		private function initGUI():void
		{
			var resource:MovieClip	=	ResManager.newResClass(_skin) as MovieClip;
			
			if(_skin!=null)
			{
				//背景
				_bgSpace	=	resource;
				_bgSpace.alpha	=	1;
				_bgSpace.x  -= _offsetX;
				_bgSpace.y  -= _offsetY;
				//关闭按钮

				//标题
				addChild(_bgSpace);	
				if(_titleRes!=""&&_titleRes!=null&&_titleRes!="null")
				{
					_titleSpace	=	new Clip(false,updataTitleSize);
					_titleSpace.source	=	StaticIni.getIniVar("res_img_str","path")+_titleRes+".png";
//					_titleSpace.mouseChildren	=	false;
//					_titleSpace.mouseEnabled	=	false;
					addChild(_titleSpace);
				}
				updateSize();
				if(_skinbut!=null&&_skinbut!="")
				{
					_closeBtn=new Button("",34,32,_skinbut);
					
					initEvent();
					addChild(_closeBtn);
				}
				if(_skinHelp!=null&&_skinHelp!="")
				{
					_helpBtn=new Button("",26,26,_skinHelp);
					
					initHelpEvent();
					addChild(_helpBtn);
				}	
			}
		}		
		private function updateSize():void
		{
			if(_canDrag)
			{
				//addDrag();
			}		
			

			if(_titleSpace!=null)
			{
				updataTitleSize();
			}
			else
			{
				setBgInitPos();
			}
		}		
		
		/**
		 * 设置窗体初始位置(有光边缘_hasBorderSpace==0、无光边缘_hasBorderSpace!=0) 
		 * 
		 */		
		private function setBgInitPos():void
		{
			if(_hasBorderSpace!=0)
			{
				_bgSpace.x  = 0;
				_bgSpace.y  = 0;
				_bgSpace.width	=	_cWidth - _offsetX * 2;
				_bgSpace.height	=	_cHeight- _offsetY * 2;
			}
			else
			{
				_bgSpace.width	=	_cWidth;
				_bgSpace.height	=	_cHeight;
			}
		}
		
		private function updataTitleSize(val:Clip=null) :void
		{
			setBgInitPos();
			if(_titleX==0)
			{
				if(_hasBorderSpace!=0)//无边缘
				{
					_titleSpace.x	=	(_bgSpace.width -_titleSpace.width)/2;
				}
				else//有边缘
				{
					_titleSpace.x   =   (_bgSpace.width - _offsetX * 2 - _titleSpace.width)/2;
				}
				
			}
			else
			{
				_titleSpace.x	=_titleX;
			}
			if(_titleY==0)
			{
				_titleSpace.y	=	-(_titleSpace.height/2) - 3;
			}
			else
			{
				_titleSpace.y	=_titleY;
			}
		}
		private function initHelpEvent():void
		{
			_helpBtn.x  = 0;
		}
		private function initEvent():void
		{
			if(_hasBorderSpace!=0)//无边缘
			{
				_closeBtn.x =   (_bgSpace.width - _closeBtn.width)/2;
			}
			else//有边缘
			{
				_closeBtn.x	=	_cWidth-2*_offsetX-_closeBtn.width - 5;
			}
			
			_closeBtn.y =	-(_closeBtn.height/2);
			_closeBtn.addEventListener(MouseEvent.CLICK,onClickClose);
		}	
		/**
		 * 发送关闭事件 
		 * 窗体组件不处理
		 * @param e
		 * 
		 */
		private function onClickClose(e:MouseEvent):void
		{
			dispatchEvent(new WindowEvent(WindowEvent.WINDOW_CLOSE));
		}
		
		private function onDragStart(e:MouseEvent):void
		{
			startDrag();
		}
		
		public function onDragStop(e:MouseEvent):void
		{
			stopDrag();
		}
		
		public function removeClose():void
		{
			if(_closeBtn!=null)
			{
				if(_closeBtn.parent!=null)
				{
					_closeBtn.parent.removeChild(_closeBtn);
				}
			}
		}	
		
		public function get offsetX():Number
		{
			return _offsetX;
		}
		
		public function get offsetY():Number
		{
			return _offsetY;
		}
		
		/**
		 * 添加拖动 
		 */
		public function addDrag():void
		{
			if(_titleSpace!=null)
			{
				_titleSpace.addEventListener(MouseEvent.MOUSE_DOWN, onDragStart);
				_titleSpace.addEventListener(MouseEvent.MOUSE_UP, onDragStop);
			}
			_bgSpace.addEventListener(MouseEvent.MOUSE_DOWN, onDragStart);
			_bgSpace.addEventListener(MouseEvent.MOUSE_UP, onDragStop);	
		}
		
		/**
		 * 移除拖动 
		 */
		public function removeDrag():void
		{
			stopDrag();
			if(_titleSpace!=null)
			{
				_titleSpace.removeEventListener(MouseEvent.MOUSE_DOWN, onDragStart);
				_titleSpace.removeEventListener(MouseEvent.MOUSE_UP, onDragStop);	
			}
			
			_bgSpace.removeEventListener(MouseEvent.MOUSE_DOWN, onDragStart);
			_bgSpace.removeEventListener(MouseEvent.MOUSE_UP, onDragStop);	
			_canDrag = false;
		}
		
		/** 
		 * 删除窗体
		 */		
		public function remove():void
		{
//			removeEvent();			
			if(this.parent != null)
			{
				this.parent.removeChild(this);
			}
		}
		public function get title():String
		{
			return _title;
		}
		public function set title(value:String):void
		{
			_title = value;
			if(_titleSpace==null)
			{
				_titleSpace	=	new Clip(false,updataTitleSize);
				_titleSpace.source	=	StaticIni.getIniVar("res_img_str","path")+value+".png";
				_titleSpace.mouseChildren	=	false;
				_titleSpace.mouseEnabled	=	false;
				addChild(_titleSpace);			
			}
			else
			{
				if(!value)
				{
					_titleSpace.clear();
				}
				else
				{
					_titleSpace.source	=	StaticIni.getIniVar("res_img_str","path")+value+".png";
				}
			}
			
		}
		override public function set width(value:Number):void
		{
			_cWidth = value;
			updateSize();
		}
		override public function set height(value:Number):void
		{
			_cHeight = value;
			updateSize();
		}
		
		override public function get height():Number
		{
			return _cHeight;
		}
		
		override public function get width():Number
		{
			return _cWidth;
		}
		
		public function get source():String
		{
			return _source;
		}
		public function set source(value:String):void
		{
			_source = value;
			_titleSpace.source	=	StaticIni.getIniVar("res_img_str","path")+_source+".png";
		}

		public function set titleX(value:int):void
		{
			_titleX = value;
			_titleSpace.x=_titleX;
		}
		public function get titleX():int
		{
			return _titleX;
			
		}
		public function set titleY(value:int):void
		{
			_titleY = value;
			_titleSpace.y=_titleY;
		}
		public function get titleY():int
		{
			return _titleY;
		}
		
		public function set hasBorderSpace(value:int):void
		{
			_hasBorderSpace = value;
		}

		public function get hasBorderSpace():int
		{
			return _hasBorderSpace;
		}
	}
}